package com.bbs.controller;

import com.bbs.resp.ResultMsg;
import com.bbs.service.HdfsService;
import com.bbs.utils.RenameUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.Map;

/**
 * @author: ZackYoung
 * @time: 2020/11/15
 */
@Controller
public class HdfsTestController {
    //    /**
//     * 读取HDFS文件转换成Byte类型
//     * @param path
//     * @return
//     * @throws Exception
//     */
//    @PostMapping("/openFileToBytes")
//    public ResultMsg openFileToBytes(@RequestParam("path") String path) throws Exception {
//        byte[] files = HdfsService.openFileToBytes(path);
//        return new ResultMsg(ResultMsg.SUCCESS, "读取HDFS文件转换成Byte类型", files);
//    }


    /**
     * 读取文件列表
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/listFile")
    public ResultMsg listFile(@RequestParam("path") String path) throws Exception {
        if (StringUtils.isEmpty(path)) {
            return new ResultMsg(ResultMsg.FAILURE, "请求参数为空");
        }
        List<Map<String, String>> returnList = HdfsService.listFile(path);
        return new ResultMsg(ResultMsg.SUCCESS, "读取文件列表成功", returnList);
    }

    /**
     * 重命名文件
     *
     * @param oldName
     * @param newName
     * @return
     * @throws Exception
     */
    @PostMapping("/renameFile")
    public ResultMsg renameFile(@RequestParam("oldName") String oldName, @RequestParam("newName") String newName)
            throws Exception {
        if (StringUtils.isEmpty(oldName) || StringUtils.isEmpty(newName)) {
            return new ResultMsg(ResultMsg.FAILURE, "请求参数为空");
        }
        boolean isOk = HdfsService.renameFile(oldName, newName);
        if (isOk) {
            return new ResultMsg(ResultMsg.SUCCESS, "文件重命名成功");
        } else {
            return new ResultMsg(ResultMsg.FAILURE, "文件重命名失败");
        }
    }

    /**
     * 删除文件
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/deleteFile")
    public ResultMsg deleteFile(@RequestParam("path") String path) throws Exception {
        boolean isOk = HdfsService.deleteFile(path);
        if (isOk) {
            return new ResultMsg(ResultMsg.SUCCESS, "delete file success");
        } else {
            return new ResultMsg(ResultMsg.FAILURE, "delete file fail");
        }
    }

    /**
     * 上传文件
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/uploadFile")
    public ResultMsg uploadFile(@RequestParam("path") String path)
            throws Exception {
        String uploadPath = "test" + "/" + RenameUtil.toDate();
        HdfsService.uploadFile(path, uploadPath);
        return new ResultMsg(ResultMsg.SUCCESS, "upload file success");
    }

    /**
     * 下载文件
     *
     * @param path
     * @param downloadPath
     * @return
     * @throws Exception
     */
    @PostMapping("/downloadFile")
    public ResultMsg downloadFile(@RequestParam("path") String path, @RequestParam("downloadPath") String downloadPath)
            throws Exception {
        HdfsService.downloadFile(path, downloadPath);
        return new ResultMsg(ResultMsg.SUCCESS, "download file success");
    }

    /**
     * HDFS文件复制
     *
     * @param sourcePath
     * @param targetPath
     * @return
     * @throws Exception
     */
    @PostMapping("/copyFile")
    public ResultMsg copyFile(@RequestParam("sourcePath") String sourcePath, @RequestParam("targetPath") String targetPath)
            throws Exception {
        HdfsService.copyFile(sourcePath, targetPath);
        return new ResultMsg(ResultMsg.SUCCESS, "copy file success");
    }

    /**
     * 查看文件是否已存在
     *
     * @param path
     * @return
     * @throws Exception
     */
    @PostMapping("/existFile")
    public ResultMsg existFile(@RequestParam("path") String path) throws Exception {
        boolean isExist = HdfsService.existFile(path);
        return new ResultMsg(ResultMsg.SUCCESS, "file isExist: " + isExist);
    }
}
